<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      table {
        width: 500px;
        text-align: center;
        border-collapse: collapse;
      }
      th,
      td {
        border: 1px solid purple;
      }
    </style>
  </head>

  <body>
    <table>
      <thead>
        <tr>
          <th>订单编号</th>
          <th>姓名</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
        </tr>
      </thead>
      <tbody>
        <!-- <tr>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
          <td>5</td>
        </tr> -->
      </tbody>
    </table>
    <div class="total">
      <div>合计：<span class="amount">1000.00</span></div>
    </div>
  </body>
  <script>
    // 2、将以下数据使用 forEach 遍历渲染到表格中（自己实现表格），并使用 reduce 方法计算出商品列表里面的总价
    const goodsList = [
      { id: 1, goodsName: '小米13pro', price: '3999', num: 2 },
      { id: 2, goodsName: '卫龙大面筋', price: '5.9', num: 10 },
      {
        id: 3,
        goodsName: 'JavaScript 高级程序设计（第四版）',
        price: '120.9',
        num: 1,
      },
      { id: 4, goodsName: 'MacBook Pro M2芯片', price: '20000', num: 1 },
    ]
    const tbody = document.querySelector('tbody')

    function render(data) {
    tbody.innerHTML= data.map(({id,goodsName,price,num})=>{
        return ` <tr>
          <td>${id}</td>
          <td>${goodsName}</td>
          <td>${price}</td>
          <td>${num}</td>
          <td>${((price*10*num)/10).toFixed(1)}</td>
        </tr>`
      
     }).join('')
    }
    render(goodsList)
    document.querySelector('.amount').innerHTML=goodsList.reduce((sum,item)=>{
      return sum+((item.price*item.num*100)/100)
    },0).toFixed(2)
  </script>
</html>
